package com.UnionPayCenter.ali.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;

/**
 * @author noah
 * @since 1.0.0, 2019/09/09
 */
@Slf4j
public class AliUtils {

    public static DefaultAlipayClient getCertAlipayRequest() throws AlipayApiException {
        String URL = "https://openapi.alipay.com/gateway.do";
        String APP_ID = "2019082566435321";
        String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZxpEjfdkWXS9JaCnuA+vsH/TJ+PKVw1jfmeFvtx0Za+yxouzqKWSbENr2VPWF2BETOJaUp2+B0DSB1dAEsfTZYQRFaPm+QX1XqxJpLQ3A5gnxsAD9CntCkR53q/UGD8YR2P9+dqaKFFe/rZg0zFgEj3wjVlgcaHvdPD1PBCE/8zkLGlT6JJd6rLiddF5RXChF7YhrjBf5dVw7VgRBnqgfHM9+qCbLsp2eVU/tFIJxV/RgMQEvoxWAfd6nzvADoIbIrdoLJX4kmnduEUvWHCQdo2LqVwjBIk/9oTLeMWmJhPyccpZWLe/tuLcy2h3+C/GFDHe4InlCqpe8vKa6ePRRAgMBAAECggEAEw6yyg08rDLcO2K3X5II2PiU47sAhWLyTrPJZojxCEeBy/n/Fn4Ow9Dw8fpRe0btxs4Ki128yf+9oiGXaB2N1KrbVqbN3LBNEmJXI8W7oJ5huRww4ys7X2KQvhAHi7rEt02HTGrz9MTUoyKmbFSuy9HkdpSq0sp4H1Dp4DMI093lGDxPMaBx71N6QXrkxhv9Qcq/iVlzc/7xLJHQHeA6UTznLnCDZI82ErzRraxm3m3/7WNFCCXtzbf8THs6oQjKkx9gvUmMt7ORpJs9edN30+ix3ciUWK2Ayi9jX3yiJHn7oGOTUWaKb8VJ+a+QCotKYN7/IGKyDBuCBIE7ra7vyQKBgQDkRP3gP8PB0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MX2kKwynoKK8fllwXM+80NKH6eanm/nLQ3IT1TVDhSk+z/LKyCO9OSPcRxV/Phe798ftTQ6u6PnvRnyMliIYlxaCR6Y38vkqijAJgTmB/rAJushFtVCB5P8dS1fesR2315FJMEJ3U404nT1BoB/g9+f3KX9zCt98lECgYB9S1Jv1hfG6ItdKk7EtIB8q0vfqyB3uqTH3jlCEwpoQMO0aYbiffKOGGCtncoY9OkzM8CehfRKU6XsH+pQFDk7UsoVzvhAOgnra+n0NDInhO7zQWRYWNVM3710x08yOrBELvvGcVqrXsgXxf7OnMvSnzKOX8fwP/fkGX1jUVWb+w==";

        String FORMAT = "json";
        String CHARSET = "UTF-8";
        String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0aYm03ghEhKBX45T1MQd7fopJj7CoN6K2wc13Ow1YKAIFzmByXzVvPiQM4t1FljL6yApei3GvGr8WjVJFHokUl5YicJy2oVDhs+IZhSMQDJ8oW40sjROJL4JiJCukyfEIHNjo9CY7xrSUYz24NBxFdZZstMd0cwMFMvTvj54KgwlmokPyaxbW+QEsPTzvpISfnGVnzeOfacYeY7EeiuDWO6KXGSZ0LLX3wK1ke1Pw4TeJzX+s0wpJZ54lHb8PywsUK5CuNkUE+7pfW5cMhHAR/vokg2S9rtfsAFgCtH/+zZxJkV5zMoYw4JmD6XfPllM0xWQie22TCQ9gVPVaduawwIDAQAB";
        String SIGN_TYPE = "RSA2";
        //构造client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        try {
            String app_cert_path = ResourceUtils.getFile("classpath:appCertPublicKey_2019082566435321.crt").getPath();
            String alipay_cert_path = ResourceUtils.getFile("classpath:alipayCertPublicKey_RSA2.crt").getPath();
            String alipay_root_cert_path = ResourceUtils.getFile("classpath:alipayRootCert.crt").getPath();

            certAlipayRequest.setServerUrl(URL);
            certAlipayRequest.setAppId(APP_ID);
            certAlipayRequest.setPrivateKey(APP_PRIVATE_KEY2);
            certAlipayRequest.setFormat(FORMAT);
            certAlipayRequest.setCharset(CHARSET);
            certAlipayRequest.setSignType(SIGN_TYPE);
            certAlipayRequest.setCertPath(app_cert_path);
            certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
            certAlipayRequest.setRootCertPath(alipay_root_cert_path);
        } catch (FileNotFoundException ex) {
            log.error("未找到证书文件");
        }
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        return alipayClient;

    }


    public static DefaultAlipayClient getSecKeyAlipayRequest() throws AlipayApiException {


        String URL = "https://openapi.alipay.com/gateway.do";
        String APP_ID = "2019091367333723";
        String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbrDvUG0iS1uiz5Mx2ccYyaCu2fR5WvdHiGBV8YbTVM5gIdk3fsbkcwfT4t200H8MCC8fFreNZYEqkfU6uaLelBsW1OREbZb3JgIjCG8RkNU9ANFUD0tJTykBhf7Wen2FlcqgPppqavhLfw3n0fyHB7QSvsMFUbKa+sitXFZo/NIUSjtUB7Awor0zDxWsCHQGYZKA1NsQxwzSnRgW1OvtjZxGxnCFk+kJf+XbOSoZF1hc5l1R+vp53+aEz9VAAMrnRSf1d2Q3TT355wvpOU9qjJQxS3eL+3tbIbXEF0qSmvDtMBVSc5lkxfKdUNK+SNbcqIBQRVxgoCrcMFbFRWYaBAgMBAAECggEAITWsCcLMR68bM41i5P2+l3BLqwGkIEMcbExRARji9/yquVtrn4YNhIoIFasj29POyTf0JULEVoPJSLAGwgRK8SSxtsh20s5wn3URN7+utOX+gybm4Arwnblu5J3XBADiST8dhUE4iDx2UFAmSzd33yQONODTTYxkQhhGUKymHEdjS+K+5wk/QR8fT/LSI+keIXrs2uTHQEwWIATAFCJOMEKblBVdumpIMNpg+rUAKxhf1G5i4KMKkyMAYdiTktXp8TFYNnDRi2qt5OCCXn+EA81wcYcMg+yfgM7bQi7R1aYQxJRjUQN+wye7G0X8MGIPYChoDe6CxXaGVK/UkYKj8QKBgQD+j1xMHX2502ab8+zxe8yEfkjVzdaRm9NnrzLHJi0f6SZ61vVGR391qSX+7Ft0IRgGhj+p6KsmT8HNYZPPujULB7t4Ldtv762jm8q4qCvx1kNt8xL3UK2TOluEk6J6JxYDDYz8c8dRpkiyxfzpNWPqqvWnkZGvt4z4Bb91rjeR/QKBgQCcjauW+iUANBkqtDsYAeyfQjB5tmR/eGn2BPfU6LIUMX1m0BZs9npeD5qB14ipg+R7b7ezGahB047LCRf1SeaGIswSrVggSu+0reparfCjgd94yIBzZhno+6bzu6FhQcecTKhuCM53kv6FMMuyizbEhxRyf7ACI4bG7ljYwQL71QKBgDl/gjaDFsnpcSzarPNipjBrIeKBKu0ayGaMESkge2LQ0+s9MIueTLDz8uwg0QtrCjU3D5w+PKJbs4fDeZKbGa4TIgokOYOQHKVu5BxPiBWI8xQbqnYIWByAOvm4KyPyPiuUXPsM38GWkKcox1aZw6bW1MPzwndxOzsueS+ImW61AoGBAJqrLNegk2W8ExEptyhk3k4JqrLVDD0TzjwNdHyY2TcyHd6xvzYv3X2IlPGq643ujJGXMjp0ItPGG9FkTpM8S9BCGLRrJiHhZylumZa1OoWQDiTakm1wdjDET5GF0k6wGnHFGrJ7aOCiDWuxZc4j1BweCImGtXGnf8SSPp0HM9dxAoGAWYWUssQX1ifk/+DbDx1SEw5nBP2NqQ4qZVuqF7BAa29e5ErM3bIzlsuMM2n4I1D/AbBAmjN91OhqMV8IUkjw1a+6OPHNwfb24WRlD+jSEzojzO0g4ZEQrYb138pfKKiboKRoSsmOfUgJx+Bd8AldZF9AmmzX6PiVbKWdnftmFOw=";
        String FORMAT = "json";
        String CHARSET = "GBK";
        String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA31LAnxqlNwMVqR5I8rGbVvJE9HbMBqOWVFYzew8Y7C82JxsykFN0D/89Rrv+GNMrH41ffIbmA1nvwnzqiFhcdXfATZZoXkDE8KGMXANLmAtMzyu8nQJcxpKBiB6XkrFCI+PdB9lIXyEtPGYQM4T+DXaycybc5onzQmVRhjh2WZhaX60/CmCl2QxeUDqK3ELGv3O7YrFp5XBvayQmbLF9LvsmDv6AYu483Sv2S1AhV36dmXpH/OUymel/Y5wPkJY9gnJQvf4/SdivSLHcTOt/fBHwXggsXM9DEUbrJPYf2q3Iy8zNL8PxK7Bkf2xdstsQ7XR/MbAEkG3Vruh+bLK1vwIDAQAB";
        String SIGN_TYPE = "RSA2";

        AlipayClient alipayClient = new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);

       // AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        return (DefaultAlipayClient) alipayClient;

    }
}
